/**
 * Created by WH on 2017/11/22.
 */
$(function () {

    $('#type-title').text($('.swiper-wrapper dl:first').attr('type-title'));
    $("#totnum").text($('.select-num').length);
    var intDiff = parseInt(6000);//计时总分钟
    //window.sessionStorage.setItem('intDiff', intDiff);
    var minute = 0,
        hourse = 0,
        second = 0; //时间默认值
    var questionAnswer = '';
    timer();
    function timer() {
        window.setInterval(function () {
            if (intDiff < 0) {
                $(".maskwhite").show();
                $(".subjuan").hide();
                $(".subjuanYes").show();
                e.preventDefault();
            }
            else {
                hourse = Math.floor(intDiff / 3600);
                minute = Math.floor((intDiff - (hourse * 3600))/60);
                second = Math.floor(intDiff) -(hourse * 3600) - (minute * 60);
                intDiff--;
            }
            if (hourse <= 9) hourse = '0' + hourse;
            if (minute <= 9) minute = '0' + minute;
            if (second <= 9) second = '0' + second;
            $('#last-question-time').html(hourse + ":" + minute + ':' + second);
            if (intDiff / 60 > 0) {
                $("#subTime").text(Math.ceil(intDiff / 60));
            } else {
                $("#subTime").text(0);
            }
        }, 1000);
    }

    //题目轮播
    var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        prevButton: '.swiper-button-prev',
        nextButton: '.swiper-button-next',
        paginationClickable: true,
        paginationBulletRender: function (index, className) {
            $("#totnum").text(index + 1);
            var html = '';
            html += '<span class="' + className + '">' + (index + 1) + '</span>';
            return html;//返回所有的页码html
        },
        onSlideChangeStart: function (swiper) {
            questionAnswer = '';
            // $("#curnum").text(swiper.activeIndex + 1);//当前页
            // var num  = $('.swiper-slide-active').attr('data-num')
            $('#type-title').text($('.swiper-slide-active').attr('type-title'));
            $("#curnum").text($('.swiper-slide-active').attr('data-num'));//当前页
            $("#totnum").text($('.swiper-slide-active').attr('data-total'));//一共几页
            var isCollection = $('.swiper-slide-active').attr('data-iscollection');
            if(isCollection=='1'){
                //0：为收藏 1：已收藏
                $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_yishoucang.png');
                $('.collectionTab').text('已收藏')
            } else {
                //0：为收藏 1：已收藏
                $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_shoucang.png')
                $('.collectionTab').text('收藏')
            }
        }
    });

    //刚进页面初始化收藏按钮样式
    installCollection();
    function installCollection() {
        var isCollection = $('.swiper-slide-active').attr('data-iscollection');
        if(isCollection=='1'){
            //0：为收藏 1：已收藏
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_yishoucang.png');
            $('.collectionTab').text('已收藏')
        } else {
            //0：为收藏 1：已收藏
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_shoucang.png')
            $('.collectionTab').text('收藏')
        }
    }

    //点击底部出现题目数card-close
    $("#numok , .back-paper").click(function () {
        $(".maskwhite").toggle();
        $(".card-top").toggle();
    });

    // /*点击答题卡标题号*/
    $('.select-num').click(function () {
        questionAnswer = '';
        var slideIndex = parseInt($(this).text()) - 1;
        $("#curnum").text($(this).text());
        $('#type-title').text($(".swiper-wrapper dl").eq(slideIndex).attr('type-title'));
        swiper.slideTo(slideIndex, 0, false);
        $(".maskwhite").toggle();
        $(".card-top").toggle();
    });

    //收藏取消收藏
    $("#showall").click(function () {
        var isCollection = $('.swiper-slide-active').attr('data-iscollection');
        if(isCollection=='0'){
            //0：为收藏 1：已收藏
            $('.swiper-slide-active').attr('data-iscollection','1');
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_yishoucang.png')
            $('.collectionTab').text('收藏')
        } else {
            //0：为收藏 1：已收藏
            $('.swiper-slide-active').attr('data-iscollection','0');
            $('.collection-img').attr('src','../../images/ErrorQuestionList/icon_shoucang.png')
            $('.collectionTab').text('已收藏')
        }
    });


    //单选择答案
    $("dl.option-one dd").click(function () {
        $(this).parent("dl").find("dd").removeClass("chance");
        $(this).parent("dl").find(".option-img").css({'border':'1px solid #666','color':'#666','background-color':'transparent'});
        $(this).addClass("chance");
        $(this).find('.option-img').css({'border':'1px solid #ffa930','color':'#fff','background-color':'#ffa930'});
        var indexnum = $(this).parent("dl").index();
        $(".select-num").eq(indexnum).addClass("choose-option");
        $('.swiper-button-next').click();
        var answerArr = '';
        $(this).parent().find('dd.chance').each(function (index,item) {
            answerArr = $(item).attr('question-option');
        });
    });

    //多选择答案
    $("dl.option-two dd").click(function () {
        if ($(this).hasClass('chance')) {
            $(this).removeClass("chance");
            $(this).find('.option-img').css({'border':'1px solid #666','color':'#666','background-color':'transparent'});
        } else {
            $(this).addClass("chance");
            $(this).find('.option-img').css({'border':'1px solid #ffa930','color':'#fff','background-color':'#ffa930'});
        }
        if ($(this).parent("dl").find("dd").hasClass("chance")) {
            var indexnum = $(this).parent("dl").index();
            $(".select-num").eq(indexnum).addClass("choose-option");
        } else {
            var indexnum = $(this).parent("dl").index();
            $(".select-num").eq(indexnum).removeClass("choose-option");
        }

        var answerArr = '';
        $(this).parent().find('dd.chance').each(function (index,item) {
            answerArr = answerArr + $(item).attr('question-option');
        });
    });

    //简答题目失去焦点事件
    $('.short-question-box').blur(function () {
        alert($(this).val());
        //获取题目id
        var questionid = $(this).attr('question-id');
        //简答题目变色
        var indexnum = $(this).parent("dl").index();
        $(".select-num").eq(indexnum).addClass("choose-option");
    });

    clickItem('.submit-paper',function () {
        alert('jiapojuan')
    })


    /*获取答题卡选项*/
    function subCard() {
        questionDetail.QuestionList = [];
        var selectNum = $('.select-num.choose-option');
        for (var i = 0; i < selectNum.length; i++) {
            var optionDetail = {};
            var QuestionId = selectNum.eq(i).attr('question-id');
            var QuestionType = selectNum.eq(i).attr('question-type');
            var userAnswer = selectNum.eq(i).attr('question-answer');
            optionDetail.QuestionId = QuestionId;
            optionDetail.QuestionType = QuestionType;
            optionDetail.UserAnswer = userAnswer;
            questionDetail.QuestionList.push(optionDetail);
        }
        console.log('答题记录', questionDetail)
    }

    $("#subno,.alert-close").click(function () {//取消
        $(".maskwhite").hide();
        $(".subjuan").hide();
        $('#numok .tab-label').css('color', '#333');
        $('#numok .navIcon').attr('src', '../../public/images/examView/bnt_jiaojuan_gray.png')
    });

    $("#subyes,#noTime").click(function () {
        $(".maskwhite").hide();
        $(".subjuan").hide();
        $(".subjuanYes").hide();
        subCard();
    });

    $('#backPage').click(function () {
       
    });

    $("#reSub").click(function () {
        window.location.href = "/exam/totle?PaperId=" + paperId + '&CourseId=' + courseId
    })

    $(document).on('click','.exam-question-img',function () {
        var str = $(this).attr('src');
        $('.alert-img-detail').attr('src',str);
        $('.alert-img').css('display','block');
    })

    $(document).on('click','.alert-img',function () {
        $('.alert-img').css('display','none');
    })

});